﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;
using FreeSilverlightChart;

namespace SimpleUsage
{
  public partial class Page : UserControl
  {
    public Page()
    {
      InitializeComponent();
      this.Loaded += new RoutedEventHandler(Page_Loaded);
    }

    public void Page_Loaded(object o, EventArgs e)
    {
      LoadChart(null, null);
    }

    public void LoadChart(object sender, HtmlEventArgs args)
    {
      //the default type of the chart is vertical bar-chart.
      Chart.ChartType chartType = Chart.ChartType.VBAR;

      //set all the information of the chart.
      ChartModel model = _loadChartModel();

      // create the chart 
      Chart chart = Chart.CreateChart(chartType, model);

      //set the chart to be in 2D or in 3D
      chart.IsPerspective = false;

      //set the position of the series labels
      chart.LegendPosition = Chart.LegendLocation.BOTTOM;

      //set the duration of the animation
      chart.AnimationDuration = 2.0;

      //set the formate of the 
      chart.Format = "$#,##0.00;($#,##0.00);0";

      //set the size of the chart area.
      chart.SetBounds(new Rect(0, 0, parentCanvas.Width, parentCanvas.Height));
      // add the custom component
      parentCanvas.Children.Add(chart);

      // setup the event handler so that when mouse enter show show the information of the bar. 
      chart.ChartClicked += new ChartEventHandler(ChartClicked);

      // now draw it
      chart.Draw();
    }

    private void ChartClicked(object sender, ChartEventArgs e)
    {
      alertText.Text = e.ToString();

      RectangleGeometry rg = alertRect.Data as RectangleGeometry;
      Rect rect = rg.Rect;
      double left = Math.Max(5.0, (rect.Width - alertText.ActualWidth) / 2);
      double top = Math.Max(5.0, (rect.Height - 20 - alertText.ActualHeight) / 2);
      alertText.SetValue(Canvas.LeftProperty, left);
      alertText.SetValue(Canvas.TopProperty, top);
      alert.Visibility = Visibility.Visible;
    }

    protected void ButtonMouseEnter(object sender, MouseEventArgs e)
    {
      Path path = okRect;
      LinearGradientBrush lgb = path.Fill as LinearGradientBrush;
      lgb.GradientStops[0].Color = Color.FromArgb(255, 240, 188, 77);
    }

    protected void ButtonMouseLeave(object sender, EventArgs e)
    {
      Path path = okRect;
      LinearGradientBrush lgb = path.Fill as LinearGradientBrush;
      lgb.GradientStops[0].Color = Color.FromArgb(255, 180, 196, 139);
    }

    protected void OKClick(object sender, MouseEventArgs e)
    {
      alert.Visibility = Visibility.Collapsed;
    }


    // load the data of the chart here.
    private ChartModel _loadChartModel()
    {
      // the name of the each group
      string[] _groupLabels = { "G1", "G2", "G3", "G4", "G5", "G6", "G7", "G8", "G9", "G10" };
      // the name of each series.
      string[] _seriesLabels = { "S1", "S2", "S3" };

      double[,] _chartYValues = {{135235, 155535, 141725},
                              {106765, 131725, 127868},
                              {108456, 119326, 139326},
                              {136765, 147265, 184349}, 
                              {107868, 113968, 174349},
                              {103019, 119145, 182080},
                              {149037, 156071, 187799},
                              {133013, 169140, 186010},
                              {139994, 186871, 158003}, 
                              {191246, 180196, 153177}};

      double[,] _chartXValues = {{6.1, 6.3, 6.5},
                                {6.8, 7.1,7.3},
                                {7.6, 7.8, 8.0},
                                {7.9, 8.4, 8.9},
                                {8.15, 8.45, 9.1},
                                {8.45, 8.55, 9.2}, 
                                {8.76, 8.75, 9.4},
                                {8.98, 8.95, 9.6},
                                {9.14, 9.25, 9.7},
                                {9.23, 9.48,9.88}};


      string[] _seriesColors = { "#E76D48", "#6EA6F3", "#9DCE6E", "#FCC46F", "#FF7FFF", "#AEAEAE" };


      Color[] colors = new Color[_seriesColors.Length];
      for (var i = 0; i < _seriesColors.Length; ++i)
      {


        if (_seriesColors[i].Length != 7)
        {
          throw new Exception("Invalid color format");
        }
        else
        {
          byte r = byte.Parse(_seriesColors[i].Substring(1, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
          byte g = byte.Parse(_seriesColors[i].Substring(3, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
          byte b = byte.Parse(_seriesColors[i].Substring(5, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
          colors[i] = Color.FromArgb(255, r, g, b);
        }
      }

      double minYValue = 0;
      double maxYValue = 200000;
      double minXValue = 6;
      double maxXValue = 10;

      string title = "This is the title of chart";
      string subTitle = "This is sub-tilte";
      string footNote = "This is footnote";

      ChartModel model = new ChartModel(_seriesLabels, _groupLabels, _chartYValues, _chartXValues, title, subTitle, footNote, colors);

      model.MaxYValue = maxYValue;
      model.MaxXValue = maxXValue;
      model.MinYValue = minYValue;
      model.MinXValue = minXValue;

      return model;
    }


  }
}

